#include "stdafx.h"
#include "GaulemSystem.h"

#include "Components\GaulemRenderableComponent.h"

#include "ResourceManager.h"
#include "EntityManager.h"

using namespace std;

GaulemSystem::GaulemSystem() :  m_pGaulem(NULL),
                                m_gaulemState(eStateUnknown),
                                m_pAnimatedComponent(NULL)
{
}


GaulemSystem::~GaulemSystem()
{
}

bool GaulemSystem::Init(GameEngine& gameEngine) 
{
    ResourceManager& resourceManager = gameEngine.GetResourceManager();
    EntityManager& entityManager = gameEngine.GetEntityManager();

    bool fSuccess = true;
    SpriteSheet* pSheet = resourceManager.GetResource<SpriteSheet>("test_spritesheet", "test_sprites");

    //Create the GAULEM
    m_pGaulem = entityManager.CreateAndAddEntity("Gaulem", "top");

    if (m_pGaulem && pSheet)
    {
        m_pAnimatedComponent = static_cast<AnimatedComponent*>(m_pGaulem->AddComponent(make_unique<AnimatedComponent>(pSheet)));
        m_pAnimatedComponent->SetZ(2);
        m_pTransformComponent = static_cast<TransformComponent*>(m_pGaulem->AddComponent(make_unique<TransformComponent>(Vector2D<f32>(500, 175))));
        m_pGaulem->AddComponent(make_unique<GaulemRenderableComponent>(true));
    }
    else
    {
        fSuccess = false;
    }

    ChangeState(eStateExplore);

    //For now just create 1 image for the top
    Entity* pEntity = entityManager.CreateAndAddEntity("top_elevator", "top");

    if (pEntity)
    {
        pEntity->AddComponent(make_unique<TransformComponent>());
        pEntity->AddComponent(make_unique<SpriteComponent>(resourceManager, "top_elevator", "top"));
        pEntity->AddComponent(make_unique<GaulemRenderableComponent>(true));
    }
    return fSuccess;
}

bool GaulemSystem::Update(ZeEngine::GameEngine& gameEngine)
{
    UpdateState();
    UpdatePhysics();

    return false;
}

void GaulemSystem::UpdatePhysics()
{
    m_pTransformComponent->SetPosition(m_pTransformComponent->GetPosition() + Vector2D<f32>(1, 0));
}

void GaulemSystem::UpdateState()
{
    switch (m_gaulemState)
    {
    case eStateExplore:
        DoStateExplore();
        break;
    }
}

void GaulemSystem::ChangeState(GaulemState eState)
{
    if (m_gaulemState != eState)
    {
        m_gaulemState = eState;

        switch (m_gaulemState)
        {
        case eStateExplore:
            ChangeToStateExplore();
            break;
        }
    }
}

//State Explore
void GaulemSystem::ChangeToStateExplore()
{
    m_gaulemState = eStateExplore;
    m_pAnimatedComponent->SetCurrentAnimation("walk_front");
}
void GaulemSystem::DoStateExplore()
{
}
